package com.obrock;

import android.content.Context;
import android.graphics.Typeface;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.*;

/**
 * Created with IntelliJ IDEA.
 * User: Дмитрий Г. Кузнецов
 * Date: 15.10.12
 * Time: 16:33
 * To change this template use File | Settings | File Templates.
 */
public class ViewAccounts extends LinearLayout {
    int bgColor;
    Context context;
    double amount = 0.0;
    double debt = 0.0;
    double normal = 0.0;

    //Account[] Main.accounts = null;
    Account selectedAccount = null;

    public ViewAccounts(Account selected, Context context) {
        super(context);
        this.context = context;
        selectedAccount = selected;
        refresh();
    }

    public ViewAccounts(Account selected, Context context, AttributeSet attrs) {
        super(context, attrs);
        this.context = context;
        selectedAccount = selected;
        refresh();
    }

    /*
    public ViewTransfers(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }
    */

    public Account refresh(Account selected) {
        selectedAccount = selected;
        return refresh();
    }

    Account refresh() {
        inflate(context, R.layout.accounts, this);
        //setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));

        this.bgColor = context.getResources().getColor(R.color.background_color);
        setBackgroundColor(bgColor);

        TableLayout container = null;
        container = (TableLayout) findViewById(R.id.container_accounts);
        container.setColumnShrinkable(1, true);
        container.setColumnStretchable(1, true);
        container.removeAllViews();


        int id = -1;
        Main.accounts = Main.db.getAccountsList();
        View current = null;

        if (selectedAccount == null && Main.accounts.length > 0) {
            selectedAccount = Main.accounts[0];
        }

        amount = 0.0;
        debt = 0.0;
        normal = 0.0;

        for (int i = 0; i < Main.accounts.length; ++i) {

            String nm = Main.accounts[i].name;
            if (Main.accounts[i].type == Account.TYPE_CARD) {
                amount += Main.accounts[i].balance;
            } else {
                amount += Main.accounts[i].balanceDebt;
            }
            debt += Main.accounts[i].balanceCredit;
            normal += Main.accounts[i].balanceDebt;

            String subTitle = Main.datetimeString(Main.accounts[i].date);
            ImageView icon = new ImageView(context);
            switch (Main.accounts[i].type) {
                case Account.TYPE_NORMAL:
                    icon.setImageDrawable(getResources().getDrawable(R.drawable.wallet));
                    break;
                case Account.TYPE_DEBT:
                    icon.setImageDrawable(getResources().getDrawable(R.drawable.credit));
                    break;
                case Account.TYPE_CARD:
                    icon.setImageDrawable(getResources().getDrawable(R.drawable.card));
                    subTitle += " " + getResources().getString(R.string.account_amount_limit_short)
                            + String.format(" %.2f", Main.accounts[i].amount);
                    break;
            }

            icon.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
            icon.setAdjustViewBounds(true);
            icon.setMaxHeight(32);
            icon.setMaxWidth(32);
            icon.setPadding(0, 4, 0, 0);

            LinearLayout iconHolder = new LinearLayout(context);
            iconHolder.addView(icon);
            LayoutParams param = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
            iconHolder.setLayoutParams(param);


            TableLayout title = new TableLayout(context);
            title.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
            title.setColumnShrinkable(1, true);
            title.setColumnStretchable(1, true);

            TextView x = Main.getTextView(context, Main.accounts[i].name);
            x.setTypeface(Typeface.DEFAULT, Typeface.BOLD);
            x.setTextSize(16);

            TableRow row = new TableRow(context);
            row.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
            row.addView(x);
            title.addView(row);

            x = Main.getTextView(context, subTitle);
            x.setTextSize(12);

            row = new TableRow(context);
            row.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
            row.addView(x);
            title.addView(row);

            //TextView type = getTextView(adapter.getItem(accounts[i].type));
            //type.setWidth(50);

            TextView balance = Main.getTextView(context, String.format("%.2f", Main.accounts[i].balance));
            balance.setGravity(Gravity.RIGHT);
            balance.setTypeface(Typeface.DEFAULT, Typeface.BOLD);
            balance.setTextSize(16);
            balance.setTextColor(Main.getAccountColor(Main.accounts[i]));

            TableRow xrow = Main.addRow(context, bgColor, i, new View[]{
                    iconHolder,
                    title,
                    balance
            });

            xrow.setTag(Main.tagAccount);

            xrow.setOnTouchListener(new View.OnTouchListener() {
                @Override
                public boolean onTouch(View view, MotionEvent motionEvent) {
                    int id = view.getId();
                    selectedAccount = Main.accounts[id];
                    Main.setObject(Main.VIEW_ACCOUNTS, selectedAccount);
                    Main.refreshCursor((ViewGroup) view.getParent(), id, bgColor);
                    return false;
                }
            });

            xrow.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View view) {
                    ViewGroup v = (ViewGroup) view.getParent();
                    int id = view.getId();

                    selectedAccount = Main.accounts[id];
                    Main.doAction(Main.VIEW_ACCOUNTS, selectedAccount);
                    /*
                    if (Main.splitMode == Main.SPLIT_DOUBLE) {
                        Main.context.refreshTransfers(getResources().getColor(R.color.pane_color));
                        //refreshCursor(v, id, bgColor);
                    } else {
                        Main.viewMode = Main.VIEW_TRANSFERS;
                        Main.context.refreshView();
                    }
                    */

                }
            });

            Main.context.registerForContextMenu(xrow);
            container.addView(xrow); //индекс можно убрать.

            if (selectedAccount != null && Main.accounts[i].accountID == selectedAccount.accountID) {
                id = i;
                current = xrow;
            }
        }


        if (id != -1) {
            Main.refreshCursor(container, id, bgColor);
            ((ScrollView) findViewById(R.id.scroll_accounts)).scrollTo(0, current.getTop());
        }

        //Main.refreshHeader();
        return selectedAccount;

        /*
        Main.context.viewHeader.set(null,
                getResources().getText(R.string.prompt_accounts).toString(), amount, debt, normal);
        */

        /*
        TextView accountTitle = (TextView) findViewById(R.id.account_title);
        //TextView accountAmount = (TextView) findViewById(R.id.account_amount);
        //TextView accountDebt = (TextView) findViewById(R.id.account_debt);
        TextView amountBalance = (TextView) findViewById(R.id.amount_balance);
        TextView amountDebt = (TextView) findViewById(R.id.amount_debt);
        TextView amountNormal = (TextView) findViewById(R.id.amount_normal);
        LinearLayout blockLines = (LinearLayout) findViewById(R.id.block_lines);
        LinearLayout blockColumns = (LinearLayout) findViewById(R.id.block_columns);

        blockLines.setVisibility(View.GONE);
        blockColumns.setVisibility(View.VISIBLE);

        ImageView icon = (ImageView) findViewById(R.id.icon);
        icon.setImageDrawable(null);

        blockLines.setVisibility(View.GONE);
        blockColumns.setVisibility(View.VISIBLE);

        accountTitle.setText(getResources().getText(R.string.prompt_accounts));
        amountBalance.setText(String.format("%.2f", amount));
        amountDebt.setText(String.format("%.2f", debt));
        amountNormal.setText(String.format("%.2f", normal));
        */

    }

    double[] getBalance() {
        return new double[] {amount, debt, normal};
    }

    /*
    public void activate() {
        Main.doAction(Main.VIEW_ACCOUNTS, selectedAccount);
    }
    */

}
